package com.my.netty.controller.user;

import com.my.netty.Utils.IWdzlJSONResult;
import com.my.netty.Utils.MD5Utils;
import com.my.netty.entrty.User;
import com.my.netty.entrty.UserVo;
import com.my.netty.server.user.UserServices;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    public UserServices userServices;

    @RequestMapping("/registerOrLogin")
    @ResponseBody
    //用户登录与注册一体化方法
    public IWdzlJSONResult registerOrlogin(User user){
        User userResult = userServices.queryUserNameIsExit(user.getUsername());
        if(userResult!=null){//此用户存在，可登录
            if(!userResult.getPassword().equals(MD5Utils.getPwd(user.getPassword()))){
                return IWdzlJSONResult.errorMsg("密码不正确");
            }
        }else{//注册
            user.setNickname(user.getUsername());
            user.setQrcode("");
            user.setPassword(MD5Utils.getPwd(user.getPassword()));
            user.setFaceImage("");
            user.setFaceImageBig("");
            userResult = userServices.insert(user);
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(userResult,userVo);
        return IWdzlJSONResult.ok(userVo);
    }

    //修改昵称方法
    @RequestMapping("/setNickname")
    @ResponseBody
    public IWdzlJSONResult setNickName(User user){
        User userResult = userServices.updateUserInfo(user);
        return IWdzlJSONResult.ok(userResult);
    }

    @RequestMapping("/getUserById")
    public String getUserById(String id, Model model){
        User user = userServices.getUserById(id);
        model.addAttribute("user",user);
        return "user_list";
    }

    @RequestMapping("/getUser")
    public String getUser(String id, Model model){
        User user = userServices.getUserById(id);
        model.addAttribute("user",user);
        return "user/userlist";
    }
}
